package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.module;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iwhalecloud.bss.kite.client.vo.scene.ModuleVO;
import com.iwhalecloud.bss.kite.common.util.KiteBeanUtils;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.DiscountModuleVO;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.DiscountVO;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.SceneNewOfferSeneVO;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.manager.scene.service.operate.LocalOperateOfferInstDiscountService;
import com.iwhalecloud.bss.kite.cucc.service.batch.BatchAcceptUtil;
import com.iwhalecloud.bss.kite.cucc.spec.impl.transfer.GoodsInfoTransfer;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInstDiscount;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.iwhalecloud.bss.kite.web.service.accept.bo.AcceptContext;
import com.iwhalecloud.bss.kite.web.service.accept.bo.SceneModule;
import com.iwhalecloud.bss.kite.web.service.accept.handler.module.AbstractModuleHandler;

@Service
public class DiscountInfoModuleHandler extends AbstractModuleHandler {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Autowired
    private LocalOperateOfferInstDiscountService operateOfferInstDiscountService;

    @Override
    public List<? extends ModuleVO> execute(AcceptContext context, SceneModule sceneModule) {

        SceneNewOfferSeneVO sceneAccountInfoVo = (SceneNewOfferSeneVO) context.getSceneVo();
        DiscountModuleVO discountVO = getDiscountInfoModule(context);
        sceneAccountInfoVo.setDiscountVO(discountVO);
        return Collections.singletonList(discountVO);
    }

    private DiscountModuleVO getDiscountInfoModule(AcceptContext context) {
        String sceneInstId = context.getSceneVo().getSceneInstId();
        // 批量受理当前修改销售品实例标识，存在则说明是批量受理的单个修改场景
        String curOfferInstId = context.getAcceptRequest().getCurOfferInstId();
        List<DiscountVO> discounts = new ArrayList<DiscountVO>();

        KiteSceneInst sceneInst = this.querySceneInstService.getSceneInst(sceneInstId);
        String rootInstId = sceneInst.getRootInstId();
        KiteOfferInst offerInst = querySceneInstService.getOfferInst(sceneInst.getCustId(), rootInstId);
        // 批量受理的单个修改，只处理当前修改的销售品实例
        if (KiteStringUtils.isNotEmpty(curOfferInstId)) {
            offerInst = querySceneInstService.getOfferInst(sceneInst.getCustId(), curOfferInstId);
        }
        String cateId = Optional.ofNullable(offerInst.getAttr("cateId")).map(KiteAttr::getDefaultValue).orElse("");
        if (!GoodsInfoTransfer.isAddRoleOffer(offerInst.getSpecId(), cateId)) {
            List<KiteOfferInstDiscount> kiteOfferInstDiscountList = operateOfferInstDiscountService.listOfferInstDiscounts(sceneInstId, context.getAcceptRequest().getCustId(), offerInst.getOfferInstId());
            if (KiteListUtils.isNotEmpty(kiteOfferInstDiscountList)) {
                KiteOfferInst finalOfferInst = offerInst;
                kiteOfferInstDiscountList.forEach(kiteDiscountInst -> {
                    DiscountVO discount = KiteBeanUtils.transform(kiteDiscountInst, DiscountVO.class);
                    discount.setDiscountInstId(kiteDiscountInst.getInstId());
                    if (KiteStringUtils.isEmpty(discount.getOfferName())) {
                        discount.setOfferName(finalOfferInst.getOfferName());
                    }
                    discounts.add(discount);
                });
            }
            else {
                DiscountVO discount = new DiscountVO();
                discount.setOfferInstId(offerInst.getOfferInstId());
                discount.setOfferName(offerInst.getOfferName());
                discounts.add(discount);
            }
        }

        DiscountModuleVO contractModuleVO = new DiscountModuleVO();
        // 批量受理部分环节，不允许编辑
        if (BatchAcceptUtil.isBatchNode(context.getAcceptRequest().getSceneInstId())
            || BatchAcceptUtil.batchNodeResetIsEdit(context.getAcceptRequest().getSceneInstId())) {
            contractModuleVO.setIsEdit("F");
        }
        contractModuleVO.setDiscounts(discounts);
        return contractModuleVO;
    }

    @Override
    public List<? extends ModuleVO> previewSceneVo(AcceptContext context, SceneModule sceneModule) {
        SceneNewOfferSeneVO sceneAccountInfoVo = (SceneNewOfferSeneVO) context.getSceneVo();
        DiscountModuleVO discountVO = getDiscountInfoModule(context);
//        if (KiteStringUtils.equals(discountVO.getIsEdit(), LocalKeyConsts.IFTRUE_T)) {
          discountVO.setIsEdit(LocalKeyConsts.IFTRUE_F);
//        }
        sceneAccountInfoVo.setDiscountVO(discountVO);
        return Collections.singletonList(discountVO);
    }
}
